﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main (string [] args)
        {
            //int length = 0;
            //for (int i = 0; ; i++)
            //{
                //string ret = aBase (i, new string[] {
                //    "0", "1", "2", "3", "4",
                //    "5", "6", "7", "8", "9", 
                //    "A", "B", "C", "D", "E",
                //    "F", "G", "H", "I", "J",
                //    "K", "L", "M", "N", "O",
                //    "P", "Q", "R", "S", "T",
                //    "U", "V", "W", "X", "Y",
                //    "Z"
                //});

                //string ret = aBase(i, new string[] { "0", "1" } );

                //if (ret.Length > length)
                //{
                //    Console.WriteLine(ret);
                //    length = ret.Length;
                //}                    
            //}

            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine(hamming32(10, 28146297));

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("fin");
            Console.ReadLine();
        }


        // algoritmo de Wegner (1960) para sacar la
        // distancia de hamming (a nivel de bit, no
        // de caracteres) de dos números enteros.
        public static int hamming32 (uint x, uint y)
        {
            uint val = x ^ y;
            int dist = 0;

            while (val != 0)
            {
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                Console.WriteLine(aBase((int)val, new string[] { "0", "1" }).PadLeft(32, '0'));

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine(aBase((int)val - 1, new string[] { "0", "1" }).PadLeft(32, '0'));

                val &= val - 1;
                dist++;

                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine(aBase((int)val, new string[] { "0", "1" }).PadLeft(32, '0'));

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
            }

            return dist;
        }

        public static string aBase (int n, string [] simbolos)
        {
            StringBuilder   ret     = new StringBuilder ();
            int             @base   = simbolos.Length;

            if (n == 0)
                ret.Append (simbolos[0]);

            while (n > 0) {
                ret.Insert (0, simbolos[n % @base]);
                n = n / @base;
            }

            return ret.ToString();
        }
    }
}
